#include "Menu.h"
#include <iostream>

//additional includes
#include <fstream>												//FPW, A1
#include <algorithm>											//MGM: B1b B2b B3b

using namespace std;

Menu::Menu()
{
}

void Menu::display_main_menu() const 
{
	cout << "Here is the MAIN MENU \n";
   /* The following code has been written by Eric Heisler, solving problem A3 */
	if(categories.size()>0 && menu_items.size()>0) {
		string menu[categories.size()][menu_items.size()+1];
		for(int i = 0; i < categories.size(); i++) {
			menu[i][0] = categories[i].display();
			int col = 1;
			for(int j = 0; j < menu_items.size(); j++) {
				if(menu_items[j].get_cat_id() == categories[i].get_cat_id()) {
					menu[i][col++] = menu_items[j].display();
				}
			}
		}
		int len = 19;
		for(int i = 0; i < menu_items.size(); i++) {
			for(int j = 0; j < categories.size(); j++) {
				cout << menu[j][i];
				for(int k = 0; k < len - menu[j][i].size(); k++) cout << " ";
				cout << "|";
			}
			cout << "\n";
			if(i == 0) {
				for(int r = 0; r < 4*len+4; r++) {
					cout << "-";
				}
				cout << "\n";
			}
		}
	}
	else cout << "Menu hasn't been read yet. Read in the menu first.\n\n";
	/* End of code written by EMH */
}
void Menu::display_info() const
{
	/* The following code has been written by Eric Heisler, solving problem A3 */
	cout << "Which item would you like to see more info about?\n>>";
	string item;
	cin >> item;
	int index = -1;
	for(int i = 0; i < menu_items.size(); i++) {
		if(menu_items[i].get_menu_item_name() == item) {
			index = i;
			break;
		}
	}
	if(index==-1) {
		cout << "Apologies, there does not appear to be an item bearing that name.\n\n";
		return;
	}
	cout << "Here is the Info requested \n";
	cout << menu_items[index].get_descr() << "\n\n";
	/* End of code written by EMH */
}

void Menu::read()
{  			
	ifstream fileinput;								//FPW, A1

	int ingredient_id;								//FPW, A1
	int recipe_id;									//FPW, A1
	float amt;									//FPW, A1
	string units;									//FPW, A1
	string name;									//FPW, A1
	string chef_name;								//FPW, A1
	string word;									//FPW, A1
	string sentence;								//FPW, A1
	int ning; //number of ingredients						//FPW, A1
	int nrec; //number of recipe & instruction					//FPW, A1
	int i=1; //iterator								//FPW, A1
	//char filename[100];								//FPW, A1
	//string category;
	//cout << "Enter the name of a file and its category:\nName>>";			//FPW, A1
	//cin >> filename;								//FPW, A1
	//cout << "\nCategory>>";
	//cin >> category;

	/* Start: MGM, A2 */
	string catmenuFilename = "data/catmenu.dat";
	string ordersFilename = "data/orders.dat";
	string recipesFilename = "data/recipes.dat";
	/* End: MGM, A2 */

											
	//Read and Store Recipe Data
	//fileinput.open(filename); // opens the file							//FPW, A1
	fileinput.open(recipesFilename.c_str());								//MGM, A2
	if(!fileinput.is_open()) { // file couldn't be opened					//FPW, A1
		cerr << "Error: file could not be opened" << endl;					//FPW, A1
	} 																		//FPW, A1
	else {																	//FPW, A1
		//if(category=="ingredients") {
		fileinput >> ning;													//FPW, A1
		//read and store the ingredients									//FPW, A1
		while (i <= ning) {													//FPW, A1
			fileinput >> ingredient_id;										//FPW, A1       
			fileinput >> recipe_id;											//FPW, A1
			fileinput >> amt;												//FPW, A1
			fileinput >> units;												//FPW, A1       
			fileinput >> name;												//FPW, A1
			Ingredient ing (ingredient_id, recipe_id, amt, units, name); 	//FPW, A1
			Menu::ingredients.push_back(ing);								//FPW, A1
			//ingredients[i-1].displaym();									//FPW, A1
			i++;															//FPW, A1
		}																	//FPW, A1
		fileinput >> nrec;													//FPW, A1
		//cout << nrec << endl;												//FPW, A1
		i = 1;																//FPW, A1
		while (i <= nrec) {													//FPW, A1
			fileinput >> recipe_id;											//FPW, A1
			fileinput >> chef_name;											//FPW, A1
			fileinput >> word;												//FPW, A1
			sentence = "";													//FPW, A1
			while (word != "#") {											//FPW, A1
				sentence = sentence + " " + word;							//FPW, A1
				fileinput >> word;											//FPW, A1
			}																//FPW, A1
			Instructions ins (sentence);									//FPW, A1
			Recipe rec (recipe_id, chef_name, ins);							//FPW, A1
			Menu::recipes.push_back(rec);									//FPW, A1
			//recipes[i-1].displaym();										//FPW, A1
			//cout << endl;													//FPW, A1
			i++;															//FPW, A1
		}																	//FPW, A1
	}
	fileinput.close();														

	//Read and Store Category and Menu Data
	fileinput.open(catmenuFilename.c_str());
	if(!fileinput.is_open()) { // file couldn't be opened                                     
		cerr << "Error: file could not be opened" << endl;			
	} 										
	else {	
		/* The following code is written by Eric Heisler, and solves the problem A3 */
		//else if(category=="menu") {
		if(Menu::menu_items.size()==0 && Menu::categories.size()==0) {
			int type;
			fileinput >> type;	
			for(int i = 0; i < type; i++) {
				int cat_id;
				string cat_name;
				fileinput >> cat_id >> cat_name;
				Category cat (cat_id, cat_name);
				Menu::categories.push_back(cat);
			}
				
			int num_menu_items;
			fileinput >> num_menu_items;
			for(int i = 0; i < num_menu_items; i++) {
				int menu_item_id;
				int cat_id;
				int recipe_id;
				string menu_item_name;
				double price;
				string descr_string;			
		
				fileinput >> menu_item_id >> cat_id >> recipe_id >> menu_item_name >> price;
					
				string next = "";
				while(next!="#") {
					fileinput >> next;
					descr_string += " " + next;
				}
				Description descr (descr_string);
					
				Menu_Item menu_item (menu_item_id, cat_id, recipe_id, menu_item_name, price, descr);
				Menu::menu_items.push_back(menu_item);
			}
		}
	}
		/* End of code written by EMH */
	fileinput.close();								//FPW, A1

	/* Start: MGM, A2 */
	//Read and Store Order and Order_Item Data
	fileinput.open(ordersFilename.c_str());
	if(!fileinput.is_open()) 
	{ 
		// file couldn't be opened
		cerr << "Error: file could not be opened" << endl;			
	} 										
	else
	{
		int numOrder, orderID, serverID ,tableID, y, m, d, h, min, numOrderItem, ordID, MenuID, prodQty;
		char seatID;
		
		//read file (Order)
		fileinput >> numOrder;
		
		for (int i = 0; i < numOrder; i++)
		{
			fileinput >> orderID >> serverID >> tableID >> y >> m >> d >> h >> min;
			
			Order temp(orderID, serverID, tableID, Date(y, m, d), Time(h, min));
			orders.push_back(temp);
		}
		
		//read file (Order Item)
		fileinput >> numOrderItem;
		
		for (int i = 0; i < numOrderItem; i++)
		{
			fileinput >> seatID >> ordID >> MenuID >> prodQty;
			
			Order_Item temp1(seatID, ordID, MenuID, prodQty);
			order_items.push_back(temp1);
		}
		cout << "\nRecipe, Ingredient, Order, Order Item, Category, and Menu Data have been stored succesfully \n\n";

	}
	fileinput.close();

	
	/* End: MGM, A2 */
	
	//cout << "File " << filename << "has been read and stored succesfully" << endl;	//FPW, A1
	//}
} 

/* MGM B.1 b: START */
//sort recipe based on chef
bool sortChef(const Recipe& left, const Recipe& right)
{
	return left.chefName() < right.chefName();
}

int findRecipe(vector<Menu_Item> menu, int id)
{
	int i = 0;
	while(menu[i].recipeID() != id && i < menu.size())
	{
		i++;
	}
	if(i < menu.size())
	{
		return i;
	} else
	{
		return -1;
	}
}
/* MGM B.1 b: END */

bool sortMenuitem(const Menu_Item& left, const Menu_Item& right)
{
	return left.getPrice() < right.getPrice();
}

void Menu::show() const 
{
    cout << "\nShow queries \n";

	/* FPW B.1 a, MGM B.1 b, EMH B.1 c: START */
	int print_menu;
	vector<Recipe> tempRecipe = recipes;
	
	cout << "\nWhat would you want to be printed? (1, 2, 3)\n1. Print a menu item recipe\n2. Print all menu items sorted by Chef\n3. print all menu items in a given category sorted by price\n>> ";
	cin >> print_menu;

	if(print_menu == 1)
	{
		if(recipes.size() <= 0 && ingredients.size() <= 0)
		{
			cout << "Recipes and ingredients have not been read and stored yet.\n\n";
		}
		else
		{
			/* The following code has been written by Fransiska Putri Wina, solving problem B1a */

			cout << "\nPrint a menu item recipe\n\n";
			string item;
			int index = -1;
			int i=0;
			int recipeid;
			
			cout << "Which menu item would you like to see its recipe?\n>> ";
			cin >> item;
			
			for(i = 0; i < menu_items.size(); i++) {
				if(menu_items[i].get_menu_item_name() == item) {
					index = i;
					break;
				}
			}
			
			if(index==-1) {
				cout << "Apologies, there does not appear to be an item bearing that name.\n\n";
			}
			else {
				recipeid = menu_items[index].recipeID();
				cout << "Here is the recipe of " << menu_items[index].get_menu_item_name()<< ": \n\n";
				cout <<  menu_items[index].get_descr() << "\n\n";
				cout << "Ingredients : \n";
				for (int i=0; i < ingredients.size(); i++) {
					if (ingredients[i].get_recipe_id() == recipeid) {
						ingredients[i].displayaun();
					}
				}
				cout << endl;
				cout << "Instructions : \n";
				for (int i=0; i < recipes.size(); i++) {
					if (recipes[i].recipeId() == recipeid) {
						recipes[i].displayins();
					}
				}
				cout << endl;
			}
			/* End of code written by FPW */
		}
	}
	else if(print_menu == 2)
	{
		/* print all menu items sorted by chef (MGM B.1 b: START) */
		
		if(menu_items.size() <= 0 && recipes.size() <= 0)
		{
			cout << "Menu items and recipes have not been read and stored yet.\n\n";
		}
		else
		{
			cout << "\nPrint all menu items sorted by Chef\n\n";
			string menu[4][menu_items.size()+1];
			menu[0][0] = " Chef Name";
			menu[1][0] = " Menu Item";
			menu[2][0] = " ID";
			menu[3][0] = " Price";
			
			//sorting recipes
			sort(tempRecipe.begin(), tempRecipe.end(), sortChef);
			//sorting menu items
			for (int i = 0; i < tempRecipe.size(); i++)
			{
				int it = findRecipe(menu_items, tempRecipe[i].recipeId());
				if(it > -1)
				{
					stringstream ss1, ss2;
					menu[0][i+1] = " " + tempRecipe[i].chefName();
					menu[1][i+1] = " " + menu_items[it].get_menu_item_name();
					ss1 << " " << menu_items[it].menuItemId();
					menu[2][i+1] = ss1.str();
					ss2 << " $" << menu_items[it].getPrice();
					menu[3][i+1] = ss2.str();
				}
			}
			
			//print menu items
			for(int i = 0; i < 4; i++)
			{
				cout << "|";
				for(int j = 0; j < 18; j++)
				{
					cout << "-";
				}
			}
			cout << "|\n";
			for(int i = 0; i < menu_items.size()+1; i++)
			{
				for(int j = 0; j < 4; j++)
				{
					cout << "|";
					cout << menu[j][i];
					int len = 18 - menu[j][i].size();
					for(int k = 0; k < len; k++)
					{
						cout << " ";
					}
					
				}
				cout << "|\n";
				if(i == 0 || i == menu_items.size())
				{
					for(int j = 0; j < 4; j++)
					{
						cout << "|";
						for(int k = 0; k < 18; k++)
						{
							cout << "-";
						}
					}
					cout << "|\n";
				}
				else
				{
					for(int j = 0; j < 4; j++)
					{
						cout << "|";
						for(int k = 0; k < 18; k++)
						{
							cout << " ";
						}
					}
					cout << "|\n";
				}
				
			}
			cout << endl << endl;
		}
		/* print all menu items sorted by chef (MGM B.1 b: END) */
	}
	else if(print_menu == 3)
	{
		if(menu_items.size() <= 0 && categories.size() <= 0)
		{
			cout << "Menu items and categories have not been read and stored yet.\n\n";
		}
		else
		{
			cout << "\nprint all menu items in a given category sorted by price\n\n";
			/* The following code has been written by Fransiska Putri Wina, solving problem B1c */

			int i;
			string cat;
			int catid;
			int index=-1;
			vector<Menu_Item> menu_item_cat;
			
			cout << "Please input your desired category :\n>>";
			cin >> cat;
			for(i = 0; i < categories.size(); i++) {
					if(categories[i].get_cat_name() == cat) {
						index = i;
						break;
					}
				}
			if (index == -1) {
				cout << "Apologies, there does not appear to be a category bearing that name.\n\n";
			}
			else {
				cout << "Here is the menu item in category " << cat << " sorted ascending by price:\n\n";
				catid = categories[i].get_cat_id();
				for(i = 0; i < menu_items.size(); i++) {
					if(menu_items[i].get_cat_id() == catid) {					
						//add new item to vector menu_item_cat for further use (sorting)
						Menu_Item menu (menu_items[i].get_menu_item_id(), catid, menu_items[i].recipeID(), menu_items[i].get_menu_item_name(), menu_items[i].getPrice(), menu_items[i].get_descr());
						menu_item_cat.push_back(menu);
					}
				}
				
				//sorting menu item in given category by price ascending
				sort (menu_item_cat.begin(), menu_item_cat.end(), sortMenuitem);
				for (i=0; i < menu_item_cat.size(); i++) {
					cout << menu_item_cat[i].get_descr() << endl << "price: " << menu_item_cat[i].getPrice() << "\n\n";
				}
			}
			/* end of code by FPW, B.1c */
		}
	}
	else
	{
		cout << "\nError: Wrong choice. Please choose 1, 2, or 3\n\n.";
	}
		
	/* FPW B.1 a, MGM B.1 b, EMH B.1 c: END */
}
void Menu::find() const 
{
    cout << "\nFind queries \n";

	/* FPW B.2 a, MGM B.2 b, EMH B.2 c: START */
	int queryMenu;
	cout << "\nWhat sales would you like to find? (1, 2, 3)\n1. Total sales for a table\n2. Total sales for a server\n3. Total sales for a menu_item\n>> ";
	cin >> queryMenu;

	if(queryMenu == 1)
	{
		if(orders.size() <= 0 && order_items.size() <= 0 && menu_items.size() <= 0)
		{
			cout << "Orders, order items, and ingredients have not been read and stored yet.\n\n";
		}
		else
		{
			/* The following code has been written by Fransiska Putri Wina, solving problem B1b */
			cout << "\nDisplay total sales for a given table\n\n";
			int table;
			int index = -1;
			int i=0;
			int orderid;
			double sales = 0;
			int itemid;
			int quantity;
			
			cout << "Please input the table number:\n>> ";
			cin >> table;
			
			for(int i = 0; i < orders.size(); i++) {
				if(orders[i].getTableID() == table) {
					index = i;
					break;
				}
			}
			
			if(index==-1) {
				cout << "Apologies, there does not appear to be a table bearing that number.\n\n";
			}
			else {
				cout << "Sales for table  " << table << ": \n\n";
				cout << "ITEM NAME\t\t\t\t\t\tPRICE\t\tQUANTITY\n";
				orderid = orders[index].getOrderID();
				for (int i=0; i < order_items.size(); i++) {
					if (order_items[i].getOrderID() == orderid) {
						itemid = order_items[i].getMenuItemID();
						quantity = order_items[i].getProdQty();
						for (int j=0; j < menu_items.size(); j++) {
							if (menu_items[j].menuItemId() ==  itemid) {
							if (menu_items[j].get_menu_item_name().length() < 9)
								cout << menu_items[j].get_menu_item_name() << "\t\t\t\t\t\t\t$" << menu_items[j].getPrice() << "\t\t\t" << quantity << "\n";
							else if (menu_items[j].get_menu_item_name().length() < 12)
								cout << menu_items[j].get_menu_item_name() << "\t\t\t\t\t$" << menu_items[j].getPrice() << "\t\t\t" << quantity << "\n";
							else
								cout << menu_items[j].get_menu_item_name() << "\t\t$" << menu_items[j].getPrice() << "\t\t\t" << quantity << "\n";
							sales = sales + menu_items[j].getPrice() * quantity;
							}
						}
					}
				}
				cout << "\nTotal sales for table  " << table << ": $" << sales << "\n\n";
			}
			/* End of code written by FPW */
		}
	}
	else if(queryMenu == 2)
	{
		//Finding total sales for a server (MGM B.2 b)

		if(orders.size() <= 0 && order_items.size() <= 0 && menu_items.size() <= 0)
		{
			cout << "Orders, order items, and menu items have not been read and stored yet.\n\n";
		}
		else
		{
			cout << "\nDisplay total sales for a given server\n\n";
			int server;
			vector<int> orderId;
			cout << "Please input the server id:\n>> ";
			cin >> server;
			
			//find order id from a given server id
			for(int i = 0; i < orders.size(); i++)
			{
				if(orders[i].getServerID() == server)
				{
					orderId.push_back(orders[i].getOrderID());
				}
			}
			if(orderId.size() == 0)
			{

				cout << "\nThere are no server: " << server << endl << endl << endl;
			}
			else
			{
				//search the order items
				vector<Order_Item> orderan;
				for(int i = 0; i < orderId.size(); i++)
				{
					for(int j = 0; j < order_items.size(); j++)
					{
						if(orderId[i] == order_items[j].getOrderID())
						{
							orderan.push_back(order_items[j]);
						}
					}
				}
				//search the menu items
				string menu[4][orderan.size()+1];
				double total = 0;
				menu[0][0] = " Order ID";
				menu[1][0] = " Menu Item";
				menu[2][0] = " Quantity";
				menu[3][0] = " Cost";
				for(int i = 0; i < orderan.size(); i++)
				{
					int j = 0;
					while(orderan[i].getMenuItemID() != menu_items[j].menuItemId() && j < menu_items.size())
					{
						j++;
					}
					if(j < menu_items.size())
					{
						stringstream ss1, ss2, ss3, ss4;
						ss1 << " " << orderan[i].getOrderID();
						menu[0][i+1] = ss1.str();
						ss2 << " " << menu_items[j].get_menu_item_name() << " $" << menu_items[j].getPrice();
						menu[1][i+1] = ss2.str();
						ss3 << " " << orderan[i].getProdQty();
						menu[2][i+1] = ss3.str();
						ss4 << " $" << menu_items[j].getPrice() * orderan[i].getProdQty();
						menu[3][i+1] = ss4.str();
						total += (menu_items[j].getPrice() * orderan[i].getProdQty());
					}
				}

				//print
				cout << "\nSales for server" << server << endl << endl;
				cout << "|";
				for(int i = 0; i < 10; i++)
				{
					cout << "-";
				}
				for(int i = 0; i < 3; i++)
				{
					cout << "|";
					for(int j = 0; j < 20; j++)
					{
						cout << "-";
					}
				}
				cout << "|\n";
				for(int i = 0; i < orderan.size()+1; i++)
				{
					for(int j = 0; j < 4; j++)
					{
						if(j == 0)
						{
							cout << "|";
							cout << menu[j][i];
							int len = 10 - menu[j][i].size();
							for(int k = 0; k < len; k++)
							{
								cout << " ";
							}
						}
						else
						{
							cout << "|";
							cout << menu[j][i];
							int len = 20 - menu[j][i].size();
							for(int k = 0; k < len; k++)
							{
								cout << " ";
							}
						}
					}
					cout << "|\n";
					if(i == 0 || i == orderan.size())
					{
						cout << "|";
						for(int j = 0; j < 10; j++)
						{
							cout << "-";
						}
						for(int j = 0; j < 3; j++)
						{
							cout << "|";
							for(int k = 0; k < 20; k++)
							{
								cout << "-";
							}
						}
						cout << "|\n";
					}
					else
					{
						cout << "|";
						for(int j = 0; j < 10; j++)
						{
							cout << " ";
						}
						for(int j = 0; j < 3; j++)
						{
							cout << "|";
							for(int k = 0; k < 20; k++)
							{
								cout << " ";
							}
						}
						cout << "|\n";
					}
				}
				cout << "| Total Cost";
				int len = 52 - 11;
				for(int i = 0; i < len; i++)
				{
					cout << " ";
				}
				cout << "|";
				stringstream ss5;
				ss5 << " $" << total;
				string totalCost = ss5.str();
				cout << totalCost;
				len = 20 - totalCost.size();
				for(int i = 0; i < len; i++)
				{
					cout << " ";
				}
				cout << "|\n";
				cout << "|";
				for(int i = 0; i < 73; i++)
				{
					cout << "-";
				}
				cout << "|\n";
				cout << endl << endl;					
			}
		}
	}
	else if(queryMenu == 3)
	{
		//Finding Total sales for a menu_item (MGM B.2 c)
		if(order_items.size() <= 0 && menu_items.size() <= 0)
		{
			cout << "Order items and menu items have not been read and stored yet.\n\n";
		}
		else
		{
			cout << "\nDisplay total sales for a given menu_item\n\n";
			int menuId;
			vector<Order_Item> orderItem;
			cout << "Please input the menu_item id:\n>> ";
			cin >> menuId;
			
			//find order id from a given menu item id
			for(int i = 0; i < order_items.size(); i++)
			{
				if(order_items[i].getMenuItemID() == menuId)
				{
					orderItem.push_back(order_items[i]);
				}
			}
			if(orderItem.size() == 0)
			{

				cout << "\nThere are no menu item with id: " << menuId << endl << endl << endl;
			}
			else
			{
				//search the menu items and price
				string menu[4][orderItem.size()+1];
				double total = 0;
				menu[0][0] = " Order ID";
				menu[1][0] = " Menu Item";
				menu[2][0] = " Quantity";
				menu[3][0] = " Cost";
				for(int i = 0; i < orderItem.size(); i++)
				{
					int j = 0;
					while(orderItem[i].getMenuItemID() != menu_items[j].menuItemId() && j < menu_items.size())
					{
						j++;
					}
					if(j < menu_items.size())
					{
						stringstream ss1, ss2, ss3, ss4;
						ss1 << " " << orderItem[i].getOrderID();
						menu[0][i+1] = ss1.str();
						ss2 << " " << menu_items[j].get_menu_item_name() << " $" << menu_items[j].getPrice();
						menu[1][i+1] = ss2.str();
						ss3 << " " << orderItem[i].getProdQty();
						menu[2][i+1] = ss3.str();
						ss4 << " $" << menu_items[j].getPrice() * orderItem[i].getProdQty();
						menu[3][i+1] = ss4.str();
						total += (menu_items[j].getPrice() * orderItem[i].getProdQty());
					}
				}

				//print
				cout << "\nSales for menu id " << menuId << endl << endl;
				cout << "|";
				for(int i = 0; i < 10; i++)
				{
					cout << "-";
				}
				for(int i = 0; i < 3; i++)
				{
					cout << "|";
					for(int j = 0; j < 20; j++)
					{
						cout << "-";
					}
				}
				cout << "|\n";
				for(int i = 0; i < orderItem.size()+1; i++)
				{
					for(int j = 0; j < 4; j++)
					{
						if(j == 0)
						{
							cout << "|";
							cout << menu[j][i];
							int len = 10 - menu[j][i].size();
							for(int k = 0; k < len; k++)
							{
								cout << " ";
							}
						}
						else
						{
							cout << "|";
							cout << menu[j][i];
							int len = 20 - menu[j][i].size();
							for(int k = 0; k < len; k++)
							{
								cout << " ";
							}
						}
					}
					cout << "|\n";
					if(i == 0 || i == orderItem.size())
					{
						cout << "|";
						for(int j = 0; j < 10; j++)
						{
							cout << "-";
						}
						for(int j = 0; j < 3; j++)
						{
							cout << "|";
							for(int k = 0; k < 20; k++)
							{
								cout << "-";
							}
						}
						cout << "|\n";
					}
					else
					{
						cout << "|";
						for(int j = 0; j < 10; j++)
						{
							cout << " ";
						}
						for(int j = 0; j < 3; j++)
						{
							cout << "|";
							for(int k = 0; k < 20; k++)
							{
								cout << " ";
							}
						}
						cout << "|\n";
					}
				}
				cout << "| Total Cost";
				int len = 52 - 11;
				for(int i = 0; i < len; i++)
				{
					cout << " ";
				}
				cout << "|";
				stringstream ss5;
				ss5 << " $" << total;
				string totalCost = ss5.str();
				cout << totalCost;
				len = 20 - totalCost.size();
				for(int i = 0; i < len; i++)
				{
					cout << " ";
				}
				cout << "|\n";
				cout << "|";
				for(int i = 0; i < 73; i++)
				{
					cout << "-";
				}
				cout << "|\n";
				cout << endl << endl;					
			}
		}
	}
	else
	{
		cout << "\nError: Wrong choice. Please choose 1, 2, or 3\n\n.";
	}
	
	/* FPW B.2 a, MGM B.2 b, EMH B.2 c: END */
}
void Menu::update()  
{
	cout << "Update queries \n";
	
	/* FPW B.3 a, MGM B.3 b, EMH B.3 c: START */
	int updateMenu;
	cout << "\nWhat update would you like to perform? (1, 2, 3)\n1. Add a new order item to an existing order\n2. Add a new menu item using an existing recipe to an existing category\n3. Add new categories Soups, Salads and Sides\n>> ";
	cin >> updateMenu;

	if(updateMenu == 1)
	{
		if(menu_items.size() <= 0)
		{
			cout << "Menu items have not been read and stored yet.\n\n";
		}
		else
		{
			cout << "\nAdd a new order item to an existing order\n\n";

			/* The following code has been written by Fransiska Putri Wina, solving problem B1c */
			
			char seat;
			int order;
			int menuitem;
			bool menu_exist=false;
			bool first=true;
			int prodq;

			cout << "Add new order item.\n";
			cout << "Please input seat number:\n>>";
			cin >> seat;
			order = order_items[order_items.size()-1].getOrderID() + 1; //get the right order item ID -- the succesor of the last order ID
			
			while (!menu_exist) {
				if (!first) {
					cout << "Apologies, there does not appear to be an menu item bearing that number.\n";
					cout << "Please re-input menu item id:\n>>";
				} else {
					cout << "Please input menu item id:\n>>";
					first = false;
				}
				cin >> menuitem;
				for(int i = 0; i < menu_items.size(); i++) {
					if(menu_items[i].menuItemId() == menuitem) {
						menu_exist = true;
						//break;
					}
				}
			}
			
			cout << "Please input product quantitiy :\n>>";
			cin >> prodq;
			
			Order_Item temp1(seat, order, menuitem, prodq);
			order_items.push_back(temp1);
			
			cout << "Order item has been added succesfully \n\n";
			
			/* End of code written by FPW */
		}
	}
	else if (updateMenu == 2)
	{
		//Add a new menu item using an existing recipe to an existing category (MGM B.3 b)
		if(categories.size() <= 0 && recipes.size() <= 0)
		{
			cout << "Categories and recipes have not been read and stored yet.\n\n";
		}
		else
		{
			cout << "\nAdd a new menu item using an existing recipe to an existing category\n\n";

			string category;
			int categoryId;
			int recipeId;
			int menuItemId;
			string menuItemName;
			double menuItemPrice;
			string description = "";
			bool catExist = false;
			bool recExist = false;
			bool showInfoCat = false;
			bool showInfoRec = false;

			//input category
			while(!catExist)
			{
				cout << "Please input a category name:\n(enter \"info\" to view the categories)\n>> ";
				cin >> category;
				for(int i = 0; i < categories.size(); i++)
				{
					if(categories[i].get_cat_name() == category)
					{
						catExist = true;
						categoryId = categories[i].get_cat_id();
						cout << endl;
					}
				}
				if(!catExist)
				{
					if(category == "info")
					{
						cout << "Categories: ";
						for(int i = 0; i < categories.size(); i++)
						{
							cout << categories[i].display();
							if(i < categories.size()-1)
							{
								cout << ", ";
							}
						}
						cout << endl << endl;
						showInfoCat = true;
					}
					else
					{
						cout << "Apologies, there does not appear to be a category.\n\n";
					}
				}				
			}
			//input recipe
			while(!recExist)
			{
				cout << "Please input a recipe id:\n(enter \"-1\" to view the recipes)\n>> ";
				cin >> recipeId;
				for(int i = 0; i < recipes.size(); i++)
				{
					if(recipes[i].recipeId() == recipeId)
					{
						recExist = true;
						cout << endl;
					}
				}
				if(!recExist)
				{
					if(recipeId == -1)
					{
						cout << "Recipes: ";
						for(int i = 0; i < recipes.size(); i++)
						{
							recipes[i].displaym();
							cout << endl;
						}
						cout << endl << endl;
						showInfoRec = true;
					}
					else
					{
						cout << "Apologies, there does not appear to be a recipe bearing that number.\n\n";
					}
				}				
			}
			//input menu item id
			cout << "Please input the menu item id:\n>> ";
			cin >> menuItemId;
			//input menu item name
			cout << "\nPlease input the menu item name:\n(Make sure the food name is 1 word long only)\n>> ";
			cin >> menuItemName;
			//input menu item price
			cout << "\nPlease input the menu item price\n(Exclude dollar symbol)\n>> ";
			cin >> menuItemPrice;
			//input description
			cout << "\nPlease input the menu description:\n(Signal the end of description by using # key as its own word)\n>> ";
			string input;
			while(input != "#")
			{
				cin >> input;
				description += input + " ";
			}
			
			//add the menu item
			Menu_Item menu (menuItemId, categoryId, recipeId, menuItemName, menuItemPrice, Description (description));
			menu_items.push_back(menu);
			cout << "\nMenu item (" << menu.get_menu_item_name() << ") has been added succesfully\n\n\n";
		}
	}
	else if(updateMenu == 3)
	{
		
		if(categories.size() <= 0)
		{
			cout << "Categories have not been read and stored yet.\n\n";
		}
		else
		{
			/* 
			The following code was written by Eric Heisler, and solves problem B3 c
			*/
			cout << "\nAdd new categories Soups, Salads and Sides\n\n";
			cout << "Please enter the name of the category you would like to add.\n(Make sure category name is 1 word)\n(Enter -1 to cancel)\n>>";
			string cat_name;
			cin >> cat_name;
			if(cat_name=="-1") {
				cout << "Cancelled.\n\n";
				return;
			}
			Category category (categories.size(), cat_name);
			categories.push_back(category);
			cout << "Category was successfully added.\n\n";
			/* End of code written by EMH */
		}
	}
	else
	{
		cout << "\nError: Wrong choice. Please choose 1, 2, or 3\n\n.";
	}
	
	/* FPW B.3 a, MGM B.3 b, EMH B.3 c: START */
   
/* 
The following code was written by Eric Heisler, and solves problem B3
*/
/*
	cout << "What would you like to update?\n(order, menu, category)\n>>";
	string update_item;
	cin >> update_item;
	
	if(update_item == "order") {
		cout << "What is your seat id?\n>>";
		char seat;
		cin >> seat;
		cout << "And what is your order id?\n>>";
		int order_id;
		cin >> order_id;
		cout << "What else would you like to order?\n>>";
		string order;
		cin >> order;
		int menu_item_id = -1;
		for(int i = 0; i < menu_items.size(); i++) {
			if(menu_items[i].get_menu_item_name() == order) {
				menu_item_id = menu_items[i].get_menu_item_id();
				break;
			}
		}
		if(menu_item_id==-1) {
			cout << "I'm sorry, we do not have that on the menu.";
			return;
		}
		cout << "And how many " + order + "s would you like?\n>>";
		int qty;
		cin >> qty;
		Order_Item order_item (seat, order_id, menu_item_id, qty);
		order_items.push_back(order_item);
		cout << "Update successful.\n\n";
	}
	else if(update_item == "menu") {
		cout << "What category of food does this item belong?\n>>";
		string cat;
		int category = -1;
		cin >> cat;
		for(int i = 0; i < categories.size(); i++) {
			if(categories[i].get_cat_name() == cat) {
				category = categories[i].get_cat_id();
				break;
			}
		}
		if(category == -1) {
			cout << "\nSorry, but there isn't a category of that name.";
			return;
		}
		int recipe_id = -2;
		while(recipe_id==-2) {
			cout << "What recipe does this food use?\n(Enter -1 to view possible recipes)\n>>";
			cin >> recipe_id;
			if(recipe_id == -1) {
				for(int j = 0; j < recipes.size(); j++) {
					recipes[j].displaym();
					cout << "\n";
				}
				recipe_id = -2;
				continue;
			}
		}
		cout << "What is the name of this food?\n(Please make sure food name is 1 word long only)\n>>";
		string name;
		cin >> name;
		cout << "How much does " + name + " cost?\n(Exclude dollar symbol)\n>>";
		double price;
		cin >> price;
		cout << "Please enter a description for " + name +".\n(Signal the end of description by using # key as its own word)\n>>";
		string desc;
		string word;
		while(word!="#") {
			cin >> word;
			desc += " " + word;
		}
		Description description (desc);
		Menu_Item menu_item (menu_items.size(), category, recipe_id, name, price, description);
		Menu::menu_items.push_back(menu_item);
		cout << "Menu item pushed back successfully.\n\n";
		cout << menu_items[menu_items.size()-1].display() << " " << menu_items[menu_items.size()-1].get_cat_id() << "\n";
	}
	else if(update_item=="category") {
		cout << "Please enter the name of the category you would like to add.\n(Make sure category name is 1 word)\n(Enter -1 to cancel)\n>>";
		string cat_name;
		cin >> cat_name;
		if(cat_name=="-1") {
			cout << "Cancelled.\n\n";
			return;
		}
		Category category (categories.size(), cat_name);
		categories.push_back(category);
		cout << "Category was successfully added.\n\n";
	}
	*/
/* End of code written by EMH */

}
bool Menu::exit() 											//FPW, A1
{															//FPW, A1
	bool confirm_bool=false; 								//FPW, A1
	char confirm; 											//FPW, A1
	cout << "Are you sure you want to exit? (y/n) \n> "; 	//FPW, A1
	cin >> confirm; 										//FPW, A1
	if (confirm == 'y' || confirm == 'Y') { 				//FPW, A1
		confirm_bool = true; 								//FPW, A1
	} 														//FPW, A1
	else if (confirm == 'n' || confirm == 'N') { 			//FPW, A1
		confirm_bool = false; 								//FPW, A1
	} 														//FPW, A1
	else {													//FPW, A1
		cout << "Invalid input. Exit aborted" << endl;		//FPW, A1
	}														//FPW, A1
	return confirm_bool; 									//FPW, A1
}															//FPW, A1